<?php
/**
* @file Postcardm.php
* @synopsis  feed流卡片相关
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015-03-31 16:52:13
*/


class Postcardm extends CI_Model
{
	function __construct()
	{
	}

	public function get_verified_card()
	{
		$verified_card_message = "";

		$mc = $this->cache->memcached;
    	$key = 'verified_card_message';

    	if(!$this->cache->memcached->is_supported())
    	{
    		$result = array("code" => -3,'tips' => "缓存未开启");
    	}

		$data_content = $mc->get($key);
		if($data_content)
		{
			$verified_card_message = $data_content;
		}
		else
		{
			$card_message = $this->db->select("message")->where("type_id",1)->get("ml_postcard")->result_array();
			$data_content = $card_message[0]['message'];
			$mc->save($key,$data_content, 5*60);
//			$this->cache->save($key,$data_content,5*60);
			$verified_card_message = $data_content;
		}
		return $verified_card_message;
	}


	public function get_invite_card()
	{
		$invite_card_message = "";

		$mc = $this->cache->memcached;
    	$key = 'invite_card_message';

    	if(!$this->cache->memcached->is_supported())
    	{
    		$result = array("code" => -3,'tips' => "缓存未开启");
    	}

		$data_content = $mc->get($key);
		if($data_content)
		{
			$invite_card_message = $data_content;
		}
		else
		{
			$card_message = $this->db->select("message")->where("type_id",2)->get("ml_postcard")->result_array();
			$data_content = $card_message[0]['message'];
			$mc->save($key,$data_content, 5*60);
//			$this->cache->save($key,$data_content,5*60);
			$invite_card_message = $data_content;
		}
		return $invite_card_message;
	}

	/**
	 * 获取crad列表
	 * @param $user_id
	 */
	public function get_card_list($user_id)
	{
		$card_list = array();
		$userinfo = $this->userm->getUserInfoById($user_id);
		$this->load->model("postcardm");
		$pcards = $this->postcardm->get_card();

		$verified_status_justice = in_array($userinfo['verified_status'], array(2, 3, 5)) ? 1 : 0;
		foreach ($pcards as $k => $v) {
			if (($verified_status_justice && $v['type_id'] == 1) || !$v['images_thumb']) continue;

			// 如果是邀请用户，
			if ($v['type_id'] == 2) {
				$uid_invite_code = $this->userm->get_uid_invite_code($this->uid);
				$v['invite_code'] = $uid_invite_code;
				$v['url'] = WEB_URL."share/invite_url/" . $this->uid;
			}

			// 2.3 版本新增类型 病例详情页/问答投票详情页
			if ($v['type_id'] == 6) {
				if (!empty($v['url'])) {
					$v['post_id'] = $v['url'];
					$post_type_extend = $this->db->select('post_type_extend')->from('ml_post')->where('id', $v['url'])->get()->row_array();
					$v['post_type_extend'] = $post_type_extend['post_type_extend'];
				}
			}

			// 2.3 专题详情页
			if ($v['type_id'] == 7) {
				if (!empty($v['url'])) {
					$v['card_id'] = $v['url'];
					$filters = array('did' => $v['url'], 'user_id' => $user_id);
					$di_subject = $this->db->select('id')->from('ml_disubject_favorite')->where($filters)->get()->row_array();
					if (!empty($di_subject['id'])) {
						$v['favorite_status'] = 1;
					} else {
						$v['favorite_status'] = 0;
					}
					$small_img = $this->db->select('small_img')->from('ml_disubject')->where('id', $v['url'])->get()->row_array();
					if (!empty($small_img['small_img'])) {
						$v['small_img'] = ret_fullpath_url($small_img['small_img']);
					} else {
						$v['small_img'] = '';
					}
					if ($_SERVER['SERVER_ADDR'] == '112.74.194.140') {
						$v['url'] = 'http://webtest.medical-lighter.com/' . $v['url'];
					} else {
						$v['url'] = 'http://www.medical-lighter.com/disubject/show/' . $v['url'];
					}
				}
			}

			$v['images_thumb'] = ret_fullpath_url($v['images_thumb']);
			$v['split_time'] = 7;
			$card_list[] = $v;
		}
		return $card_list;
	}

	public function get_card()
	{
		$invite_card_message = "";
        //先请求用户的thread
        $userinfo = $this->userm->getUserInfoById($this->uid);
        $thread = $userinfo['thread'];
        //取科室对应的card_id
		$mc = $this->cache->memcached;
        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }
        if($thread == -1)
        {
            $thread = 1;
        }

        if($thread == -2)
        {
            $thread = 2;
        }
        $thread_key = 'get_card_thread_2'.$thread;
        $card_ids = $mc->get($thread_key);
        if(!$card_ids)
        {
            $card_ids = array();
            $result = $this->db->select('postcard')->where("thread",$thread)->get("ml_postcardthread")->result_array();
            foreach($result as $value)
            {
                $card_ids[] = $value['postcard'];
            }
            $mc->save($thread_key,$card_ids,5*60);
        }

        if(!$card_ids)
        {
            return array();
        }
    	$key = 'card_message_5'.$thread;

		$data_content = $mc->get($key);
		if($data_content)
		{
			$invite_card_message = $data_content;
		}
		else
		{
            $ids = implode(',',$card_ids);
            $sql = 'SELECT * FROM ml_postcard WHERE is_show = 1 AND id in ('.$ids.') order by sort asc,id desc';
            $query = $this->db->query($sql);
            $card_message = $query->result_array();
//			$card_message = $this->db->select()->from("ml_postcard")->where("is_show",1)->where_in('id',$ids)->get()->result_array();
			$mc->save($key,$data_content, 5*60);
			$invite_card_message = $card_message;
		}

		return $invite_card_message;
	}

	public function get_card_test($type_id)
	{
		$card_message = $this->db->select()->where("type_id",$type_id)->limit(1,0)->get("ml_postcard")->result_array();
		return $card_message;
	}
}
?>
